import { _decorator, Color, Component, Label, Node, Sprite } from 'cc';
const { ccclass, property } = _decorator;

import { AIDifficulty } from "../Gomoku_AI";
import Gomoku_GameManager from "../Gomoku_GameManager";

@ccclass('GomokuDifficultyPanel')
export default class Gomoku_DifficultyPanel extends Component {
    static Instance: Gomoku_DifficultyPanel;
    @property(Node)
    Button_easy: Node | null = null;
    @property(Node)
    Button_medium: Node | null = null;
    @property(Node)
    Button_hard: Node | null = null;
    @property(Node)
    Button_close: Node | null = null;
    onLoad() {
        Gomoku_DifficultyPanel.Instance = this;

        this.addListeners();
        this.updateButtonHighlights();
    }
    addListeners() {
        if (this.Button_easy) {
            this.Button_easy.on(Node.EventType.TOUCH_END, this.On_Button_easy, this);
        }
        if (this.Button_medium) {
            this.Button_medium.on(Node.EventType.TOUCH_END, this.On_Button_medium, this);
        }
        if (this.Button_hard) {
            this.Button_hard.on(Node.EventType.TOUCH_END, this.On_Button_hard, this);
        }
        if (this.Button_close) {
            this.Button_close.on(Node.EventType.TOUCH_END, this.On_Button_close, this);
        }
    }
    On_Button_easy() {
        if (Gomoku_GameManager.Instance) {
            Gomoku_GameManager.Instance.setDifficulty(AIDifficulty.Easy);
            this.updateButtonHighlights();
            this.playButtonAudio();
        }
    }
    On_Button_medium() {
        if (Gomoku_GameManager.Instance) {
            Gomoku_GameManager.Instance.setDifficulty(AIDifficulty.Medium);
            this.updateButtonHighlights();
            this.playButtonAudio();
        }
    }
    On_Button_hard() {
        if (Gomoku_GameManager.Instance) {
            Gomoku_GameManager.Instance.setDifficulty(AIDifficulty.Hard);
            this.updateButtonHighlights();
            this.playButtonAudio();
        }
    }
    On_Button_close() {
        this.node.destroy();
        this.playButtonAudio();
    }
    //    // 更新按钮高亮状态
    updateButtonHighlights() {
        if (!Gomoku_GameManager.Instance) return;

        const currentDifficulty = Gomoku_GameManager.Instance.currentDifficulty;

        // 重置所有按钮的高亮状态
        this.resetButtonHighlights();

        // 根据当前难度高亮对应按钮
        switch (currentDifficulty) {
            case AIDifficulty.Easy:
                this.highlightButton(this.Button_easy);
                break;
            case AIDifficulty.Medium:
                this.highlightButton(this.Button_medium);
                break;
            case AIDifficulty.Hard:
                this.highlightButton(this.Button_hard);
                break;
        }
    }
    //    // 重置所有按钮的高亮状态
    resetButtonHighlights() {
        this.resetButtonHighlight(this.Button_easy);
        this.resetButtonHighlight(this.Button_medium);
        this.resetButtonHighlight(this.Button_hard);
    }
    //    // 高亮指定按钮
    highlightButton(button: Node) {
        if (!button) return;

        // 只改变按钮颜色为高亮色
        // button.color = new cc.Color(255, 215, 0, 255); // 金黄色高亮
        button.getComponent(Sprite).color = new Color().fromHEX(`#73CB39`); // 选中高亮色

        let Label_text = button.getComponentInChildren(Label);
        if (Label_text) {
            Label_text.color = new Color().fromHEX(`#FFF9C4`);
        }
    }
    //    // 重置按钮高亮状态
    resetButtonHighlight(button: Node) {
        if (!button) return;

        // 恢复默认颜色
        // button.color = Color.WHITE; // 默认颜色
        button.getComponent(Sprite).color = new Color().fromHEX(`#525963`); // 默认颜色

        let Label_text = button.getComponentInChildren(Label);
        if (Label_text) {
            Label_text.color = new Color().fromHEX(`#93A7BE`);
        }
    }
    playButtonAudio() {
        // 播放按钮音效
        // const AudioManager = require("../../../Script/Manager/AudioManager").default;
        // if (AudioManager) {
        //     AudioManager.PlayButtonAudio();
        // }
    }
}